package com.ymatou.iapi.idcardquery.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.common.lang.CipherUtil;
import com.ymatou.iapi.idcardquery.parameter.BuyeIdAndNameInfo;
import com.ymatou.iapi.idcardquery.parameter.GetBatchIdCardInfosByBuyerIdAndNameReq;
import com.ymatou.iapi.idcardquery.resp.GetBatchIdCardInfosResp;
import com.ymatou.iapi.idcardquery.resp.IdCardInfo;
import com.ymatou.iapi.idcardquery.resp.IdCardInfoVerify;
import com.ymatou.iapi.idcardquery.resp.RejectReasonEnum;
import com.ymatou.iapi.idcardquery.service.GetBatchIdCardInfosByBuyerIdAndNameCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.database.sqlwapper.YmtIdCardInfoWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
/**
*根据批量的买家Id、姓名获取身份证信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetBatchIdCardInfosByBuyerIdAndName {
	private static GetBatchIdCardInfosByBuyerIdAndNameReq bean;
	private static GetBatchIdCardInfosByBuyerIdAndNameCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据批量的买家Id、姓名获取身份证信息");
	}

	@Before
	public void caseUp() {
		bean = new GetBatchIdCardInfosByBuyerIdAndNameReq();
		call = new GetBatchIdCardInfosByBuyerIdAndNameCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDb() throws Exception {

		YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();
		String oldKey = "1234567890123456";
		String newKey = "123456789";
		GetBatchIdCardInfosResp batchIdCardInfosResp = call
				.getBatchIdCardInfosResp();

		List<IdCardInfo> actuallist = batchIdCardInfosResp.getIdCardInfoList();

		List<YmtIdCardInfo> expectedlist = new ArrayList<YmtIdCardInfo>();

		bean.getIdCardInfos().forEach(
				item -> {

					String name = item.getName();
					long buyerId = item.getBuyerId();

					YmtIdCardInfo ymtIdCardInfo = ymtIdCard
							.selectByNameAndBuyerId(name, buyerId);

					if (ymtIdCardInfo != null) {

						expectedlist.add(ymtIdCardInfo);
					}
				});

		Logger.comment("校验身份证信息表ymt_idcardinfo");

		for (int i = 0; i < expectedlist.size(); i++) {

			YmtIdCardInfo expectedIdCardInfo = expectedlist.get(i);
			IdCardInfo actualIdCardInfo = actuallist.get(i);

			Logger.comment("校验name:" + expectedIdCardInfo.getName());

			Logger.verifyEquals(expectedIdCardInfo.getCardId(),
					actualIdCardInfo.getCardId(), "校验CardId");

			Logger.verifyEquals(expectedIdCardInfo.getName(),
					actualIdCardInfo.getName(), "校验name");
			Logger.verifyEquals(
					CipherUtil.decryptAES(oldKey, expectedIdCardInfo.getIDCard()),
					actualIdCardInfo.getCardNumber(), "校验CardNumber");

			Logger.verifyEquals(expectedIdCardInfo.getCardType(),
					actualIdCardInfo.getCardType(), "校验CardType");

			Logger.verifyEquals(expectedIdCardInfo.getStatus(),
					actualIdCardInfo.getStatus(), "校验Status");

			Logger.verifyEquals(
					CipherUtil.decryptAES(newKey, expectedIdCardInfo.getMobile()),
					actualIdCardInfo.getMobile(), "校验Mobile");

			String rejectReason = null;

			int rejectReasonCode = expectedIdCardInfo.getRejectReason();

			if (expectedIdCardInfo.getStatus() == -1) {

				RejectReasonEnum rejectReasonEnum = RejectReasonEnum

				.getEnum(rejectReasonCode);

				rejectReason = "审核未通过";

				if (null != rejectReasonEnum) {
					rejectReason = rejectReasonEnum.getMessage();
				}
			}

			String expectedPicRightSide = "";

			String expectedPicReverseSide = "";

			//boolean isHasPic = true;

			if (!expectedIdCardInfo.getRightSide().isEmpty()) {

				String picUrl = IdCardInfoVerify
						.convertPicUrl(expectedIdCardInfo.getRightSide());

				expectedPicRightSide = picUrl;

			}/* else {
				isHasPic = false;

				}*/

			if (!expectedIdCardInfo.getReverseSide().isEmpty()) {

				String picUrl = IdCardInfoVerify
						.convertPicUrl(expectedIdCardInfo.getReverseSide());
				expectedPicReverseSide = picUrl;
			}/* else {
				isHasPic = false;
				}*/

			Logger.verifyEquals(expectedIdCardInfo.getHasPic(),
					actualIdCardInfo.getHasPic(), "校验HasPic");

			Logger.verifyEquals(rejectReason,
					actualIdCardInfo.getRejectReason(), "校验RejectReason");

			Logger.verifyEquals(expectedPicRightSide,
					actualIdCardInfo.getPicRightSide(), "校验picRightSide");
			Logger.verifyEquals(expectedPicReverseSide,
					actualIdCardInfo.getPicReverseSide(), "校验PicReverseSide");

			Logger.verifyEquals(expectedIdCardInfo.getCheckIdCardStatus(),
					actualIdCardInfo.getCheckIdCardStatus(),
					"校验CheckIdCardStatus");

			DataUtil.verifyEquals(expectedIdCardInfo.getUploadTime(),
					actualIdCardInfo.getUploadTime(), "验证UploadTime");

		}

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByBuyerIdAndName_001() {
		Logger.start(true, "根据批量的买家Id、姓名获取身份证信息成功");
		try {
			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));
			String name = "沈引";
			long buyerId2 = Long.valueOf(EnvSetup.getData("SellerId"));
			String name2 = "张三2";

			bean.setIdCardInfos(Arrays.asList(new BuyeIdAndNameInfo(buyerId,
					name), new BuyeIdAndNameInfo(buyerId2, name2)));

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("200", call.getString("code"), "验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByBuyerIdAndName_002() {
		Logger.start(true, "买家Id为null");
		try {

			//long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));
			String name = "黄英";

			bean.setIdCardInfos(Arrays.asList(new BuyeIdAndNameInfo(0L, name)));

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证Code");
			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");
			Logger.verifyEquals("idCardInfos[0].buyerId:buyerId未提供",
					call.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByBuyerIdAndName_003() {
		Logger.start(true, "收件人为null");
		try {
			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			bean.setIdCardInfos(Arrays.asList(new BuyeIdAndNameInfo(buyerId,
					null)));

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证Code");
			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");
			Logger.verifyEquals("idCardInfos[0].name:收件人未提供",
					call.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByBuyerIdAndName_004() {
		Logger.start(true, "idCardInfos列表null");
		try {

			bean.setIdCardInfos(null);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证Code");

			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");
			Logger.verifyEquals("idCardInfos:买手Id和姓名未提供",
					call.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByBuyerIdAndName_005() {
		Logger.start(true, "idCardInfos列表size为0");
		try {
			List<BuyeIdAndNameInfo> list = new ArrayList<BuyeIdAndNameInfo>();

			bean.setIdCardInfos(list);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证Code");

			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");
			Logger.verifyEquals("idCardInfos:买手Id和姓名未提供",
					call.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetBatchIdCardInfosByBuyerIdAndName_006() {
		Logger.start(true, "idCardInfos列表里含有不存在的用户id,收件人");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));
			String name = "沈引";
			long buyerId2 = Long.valueOf(EnvSetup.getData("SellerId"));
			String name2 = "张四";

			bean.setIdCardInfos(Arrays.asList(new BuyeIdAndNameInfo(buyerId,
					name), new BuyeIdAndNameInfo(buyerId2, name2)));

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("200", call.getString("code"), "验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}